﻿Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management

Public Module SMOUtilities
    Public Function GetSQLInstanceList(ByVal OnLocal As Boolean) As DataTable
        Return SmoApplication.EnumAvailableSqlServers(OnLocal)
    End Function

    Public Function GetDatabaseList(ByVal SQLInstanceName As String, ByVal IsWindowsAuth As Boolean, ByVal UserName As String, ByVal Password As String) As DataTable
        Try
            Dim oServer As Smo.Server = Utility.ConnectServer(SQLInstanceName, UserName, Password, IsWindowsAuth)
            Dim dt As New DataTable("Database")
            dt.Columns.Add("Name", GetType(String))
            For Each db As Database In oServer.Databases
                Dim dr As DataRow = dt.NewRow
                dr("Name") = db.Name
                dt.Rows.Add(dr)
            Next

            dt.AcceptChanges()
            Return dt
        Catch ex As Exception
            Throw New Exception("Có lỗi xảy ra trong quá trình lấy danh sách dữ liệu. Vui lòng kiểm tra lại thông tin kết nối.", ex)
        End Try

        Return Nothing
    End Function
End Module
